import socket
import struct
import sys

multicast_group = '224.0.2.2'
server_address = ('',42102)

# Create the socket
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)

# Bind to the server address
sock.bind(server_address)
ttl = struct.pack('b', 2)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
sock.settimeout(1)

# Tell the operating system to add the socket to the multicast group
# on all interfaces.
group = socket.inet_aton(multicast_group)
mreq = struct.pack('4sL', group, socket.INADDR_ANY)
sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
# Receive/respond loop
data, address = sock.recvfrom(50000)
print(data)
# filename = 'test_text.txt'
# with open(filename, 'w') as file_object:
#     file_object.write(data.decode('gbk'))
print(">>>>>")
# while True:
    # sock.send("2323")